package com.whq.thrift.api.web.admin.controller.bs;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.methodinfo.MethodInfo;
import com.whq.thrift.api.web.admin.service.verify.MethodInfoService;
import com.whq.thrift.api.web.admin.util.PageData;

@Controller
@Scope("prototype")
public class MethodInfoController {

	private static final int DEFAULT_PAGE_SIZE = 25;

	@Autowired
	private MethodInfoService methodInfoService;

	@RequestMapping(value = "bs/methodinfo/list")
	public String list(
			@RequestParam(value = "methodName", required = false) String methodName,
			@RequestParam(value = "isValid", required = false) Integer isValid,
			@RequestParam(value = "methodStatus", required = false) Integer methodStatus,
			@RequestParam(value = "introDeveloper", required = false) String introDeveloper,
			@RequestParam(value = "currentPage", required = true, defaultValue = "1") Integer currentPage,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			Model model) {
		PageData<MethodInfo> pageData = new PageData<MethodInfo>();
		pageData.setCurrentPage(currentPage);
		if ((pageSize == null) || (pageSize <= 0)) {
			pageSize = DEFAULT_PAGE_SIZE;
		}
		pageData.setPageSize(pageSize);

		// 查询参数
		Map<String, Object> whereParams = new HashMap<String, Object>(0);
		// 查询参数
		if (StringUtil.isNotBlank(methodName)) {
			whereParams.put("methodName", methodName);
		}
		if (isValid != null) {
			whereParams.put("isValid", isValid);
		}
		if (methodStatus != null) {
			whereParams.put("methodStatus", methodStatus);
		}
		if (StringUtil.isNotBlank(introDeveloper)) {
			whereParams.put("introDeveloper", introDeveloper);
		}

		int totalCount = methodInfoService.findMethodsCount(whereParams);
		pageData.setTotalCount(totalCount);

		int beginIndex = (currentPage - 1) * pageSize;
		List<MethodInfo> resultList = methodInfoService.findMethodsByMap(whereParams, beginIndex, pageSize);

		pageData.setResultList(resultList);

		model.addAttribute("methodName", methodName);
		model.addAttribute("isValid", isValid);
		model.addAttribute("methodStatus", methodStatus);
		model.addAttribute("introDeveloper", introDeveloper);
		model.addAttribute("pageData", pageData);
		return "bs/methodinfo/list";
	}

	@RequestMapping(value = "bs/methodinfo/show")
	public String show(@RequestParam(value = "id", required = true) Integer id, Model model) {

		MethodInfo method = this.methodInfoService.getMethodById(id);
		if (method == null) {
			model.addAttribute("errMsg", "缺少参数: 找不到方法!");
			return "error";
		}
		model.addAttribute("method", method);
		return "bs/methodinfo/show";
	}

	@RequestMapping(value = "bs/methodinfo/showAdd")
	public String showAdd(Model model) {
		MethodInfo method = new MethodInfo();
		model.addAttribute("method", method);
		return "bs/methodinfo/showAdd";
	}

	@RequestMapping(value = "bs/methodinfo/add")
	public String add(@Valid MethodInfo method, BindingResult result, Model model) {
		MethodInfo existMethod = this.methodInfoService.getMethodByName(method.getMethodName());
		if (method.getMethodName() == null) {
			model.addAttribute("errMsg", "方法名不能为空!");
			model.addAttribute("method", method);
			return "bs/methodinfo/showAdd";
		}

		if (existMethod != null) {
			model.addAttribute("errMsg", "该方法名已经存在,请更换!");
			model.addAttribute("method", method);
			return "bs/methodinfo/showAdd";
		}

		this.methodInfoService.addMethod(method);
		return "redirect:show.do?id=" + methodInfoService.getMethodByName(method.getMethodName()).getId();
	}

	@RequestMapping(value = "bs/methodinfo/showUpd")
	public String showUpd(@RequestParam(value = "id", required = true) int id, Model model) {
		if (id <= 0) {
			model.addAttribute("errMsg", "缺少参数: id!");
			return "error";
		}
		MethodInfo method = this.methodInfoService.getMethodById(id);
		if (method == null) {
			model.addAttribute("errMsg", "该记录不存在!");
			return "error";
		}
		model.addAttribute("method", method);
		return "bs/methodinfo/showUpd";
	}

	@RequestMapping(value = "bs/methodinfo/upd")
	public String upd(@Valid MethodInfo method, BindingResult result, Model model) {
		if (result.hasErrors()) {
			model.addAttribute("method", method);
			return "bs/methodinfo/showUpd";
		}

		this.methodInfoService.updateMethod(method);
		return "redirect:show.do?id=" + method.getId();
	}

	@RequestMapping(value = "bs/methodinfo/del")
	public String del(@RequestParam(value = "id", required = true, defaultValue = "0") Integer id, Model model) {
		this.methodInfoService.deleteMethod(id);
		return "bs/methodinfo/delOk";
	}

	@RequestMapping(value = "bs/methodinfo/delmethods")
	public String delMethods(int[] delIds, Model model) {
		if (delIds != null) {
			for (int i = 0; i < delIds.length; i++) {
				this.methodInfoService.deleteMethod(delIds[i]);
			}
		}
		return "redirect:list.do";
	}

}